package net.lab1024.smartadmin.module.business.customer.service;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import net.lab1024.smartadmin.common.domain.PageResultDTO;
import net.lab1024.smartadmin.common.domain.ResponseDTO;
import net.lab1024.smartadmin.module.business.customer.dao.CustomerDao;
import net.lab1024.smartadmin.module.business.customer.domain.dto.CustomerAddDTO;
import net.lab1024.smartadmin.module.business.customer.domain.dto.CustomerUpdateDTO;
import net.lab1024.smartadmin.module.business.customer.domain.dto.CustomerQueryDTO;
import net.lab1024.smartadmin.module.business.customer.domain.entity.CustomerEntity;
import net.lab1024.smartadmin.module.business.customer.domain.vo.CustomerVO;
import net.lab1024.smartadmin.module.business.customer.domain.vo.CustomerExcelVO;
import net.lab1024.smartadmin.util.SmartPageUtil;
import net.lab1024.smartadmin.util.SmartBeanUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * [  ]
 *
 * @author mrmf
 * @version 1.0
 * @company ${company}
 * @copyright (c)  ${company}Inc. All rights reserved.
 * @date 2021-05-08 15:09:28
 * @since JDK1.8
 */
@Service
public class CustomerService {

    @Autowired
    private CustomerDao customerDao;

    /**
     * 根据id查询
     */
    public CustomerEntity getById(Long id){
        return  customerDao.selectById(id);
    }

    /**
     * 分页查询
     * @author mrmf
     * @date 2021-05-08 15:09:28
     */
    public ResponseDTO<PageResultDTO<CustomerVO>> queryByPage(CustomerQueryDTO queryDTO) {
        Page page = SmartPageUtil.convert2QueryPage(queryDTO);
        IPage<CustomerVO> voList = customerDao.queryByPage(page, queryDTO);
        PageResultDTO<CustomerVO> pageResultDTO = SmartPageUtil.convert2PageResult(voList);
        return ResponseDTO.succData(pageResultDTO);
    }

    /**
     * 添加
     * @author mrmf
     * @date 2021-05-08 15:09:28
     */
    public ResponseDTO<String> add(CustomerAddDTO addDTO) {
        CustomerEntity entity = SmartBeanUtil.copy(addDTO, CustomerEntity.class);
        customerDao.insert(entity);
        return ResponseDTO.succ();
    }


    /**
     * 编辑
     * @author mrmf
     * @date 2021-05-08 15:09:28
     */
    @Transactional(rollbackFor = Exception.class)
    public ResponseDTO<String> update(CustomerUpdateDTO updateDTO) {
        CustomerEntity entity = SmartBeanUtil.copy(updateDTO, CustomerEntity.class);
        customerDao.updateById(entity);
        return ResponseDTO.succ();
    }

    /**
     * 删除
     * @author mrmf
     * @date 2021-05-08 15:09:28
     */
    @Transactional(rollbackFor = Exception.class)
    public ResponseDTO<String> deleteByIds(List<Long> idList) {
        customerDao.deleteByIdList(idList);
        return ResponseDTO.succ();
    }

    /**
     * 查询全部导出对象
     * @author mrmf
     * @date 2021-05-08 15:09:28
     */
    public List<CustomerExcelVO> queryAllExportData(CustomerQueryDTO queryDTO) {
        return customerDao.queryAllExportData( queryDTO);
    }

    /**
     * 批量查询导出对象
     * @author mrmf
     * @date 2021-05-08 15:09:28
     */
    public List<CustomerExcelVO> queryBatchExportData(List<Long> idList) {
        return customerDao.queryBatchExportData(idList);
    }
}
